﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XQ.Framework.model
{
    public class RssItemModelBase
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 链接
        /// </summary>
        public string Link { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 发布日期
        /// </summary>
        public string Pubdate { get; set; }
        /// <summary>
        /// 来源
        /// </summary>
        public string Source { get; set; }
        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; set; }
    }

    public class RssChannelModelBase
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 链接
        /// </summary>
        public string link { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string description { get; set; }
        /// <summary>
        /// 条目组
        /// </summary>
        public List<RssItemModelBase> items { get; set; }

        /// <summary>
        /// 获取频道的RSS文件的内容
        /// </summary>
        public string GetRSS()
        {
            StringBuilder strRtn = new StringBuilder();
            string strLine = string.Empty;

            strLine = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            strRtn.Append(strLine);

            strRtn.Append("<rss version=\"2.0\">");
            strRtn.Append("<channel>");

            strRtn.Append("<title>" + this.title + "</title>");
            strRtn.Append("<link>" + this.link + "</link>");

            if (!string.IsNullOrEmpty(this.description))
            {
                strLine = "<description>" + this.description + "</description>";
                strRtn.Append(strLine);
            }

            foreach (RssItemModelBase newsItem in this.items)
            {
                strRtn.Append("<item>");

                strRtn.Append("<title><![CDATA[" + newsItem.Title + " ]]></title>");
                strRtn.Append("<link><![CDATA[" + newsItem.Link + " ]]></link>");

                if (!string.IsNullOrEmpty(newsItem.Description))
                {
                    strLine = "<description><![CDATA[" + newsItem.Description + " ]]></description>";
                    strRtn.Append(strLine);
                }

                if (!string.IsNullOrEmpty(newsItem.Pubdate))
                {
                    strLine = "<pubDate><![CDATA[" + newsItem.Pubdate + " ]]></pubDate>";
                    strRtn.Append(strLine);
                }

                if (!string.IsNullOrEmpty(newsItem.Source))
                {
                    strLine = "<source><![CDATA[" + newsItem.Source + " ]]></source>";
                    strRtn.Append(strLine);
                }

                if (!string.IsNullOrEmpty(newsItem.Author))
                {
                    strLine = "<author><![CDATA[" + newsItem.Author + " ]]></author>";
                    strRtn.Append(strLine);
                }

                strRtn.Append("</item>");
            }

            strRtn.Append("</channel>");
            strRtn.Append("</rss>");

            return strRtn.ToString();

        }
    }
}